<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>PostMessage Parent</title>
  </head>
  <body>
    <h2>Parent</h2>
    <button id="btn">发送消息</button>
    <iframe id="iframe" src="./child.html"></iframe>
    <script>
      // 发布订阅模式
      document.getElementById('btn').addEventListener('click', () => {
        window.iframe.contentWindow.postMessage('您好，我是你爹', '*') // * 代表任何域名
      })
      // 接收消息
      window.addEventListener('message', event => {
        console.log('儿子的消息：', event.data)
      })
    </script>
  </body>
</html>
